package com.tool.builder.BuildClass;

import com.tool.bean.Constants;
import com.tool.bean.TableInfo;
import com.tool.builder.BuildHelper.BuildComment;
import com.tool.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * 创建Controller包
 */
public class BuildController {
    private static final Logger logger = LoggerFactory.getLogger(BuildController.class);

    public static void execute(TableInfo tableInfo) {
        File folder = new File(Constants.PATH_CONTROLLER);
        if(!folder.exists()) {   // 路径不存在则创建
            folder.mkdirs();
        }

        String beanName = tableInfo.getBeanName();
        String lowBeanName = StringUtils.lowerCaseFirstLetter(beanName);
        String lowServiceName = StringUtils.lowerCaseFirstLetter(beanName) + "Service";
        String serviceName = beanName + "Service";
        String id = lowBeanName + "Id";

        File controllerFile = new File(folder, beanName + "Controller.java");


        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(controllerFile);
            outw = new OutputStreamWriter(out, "utf8");
            bw = new BufferedWriter(outw);

            bw.write("package " + Constants.PACKAGE_CONTROLLER + ";");
            bw.newLine();
            bw.newLine();

            bw.write("import com.generation.tool.pojo.result.R;");
            bw.newLine();
            bw.write("import org.springframework.web.bind.annotation.*;");
            bw.newLine();
            bw.write("import java.util.List;");
            bw.newLine();
            bw.write("import org.springframework.beans.factory.annotation.Autowired;");
            bw.newLine();
            bw.write("import " + Constants.PACKAGE_PO + "." + beanName + ";");
            bw.newLine();
            bw.write("import " + Constants.PACKAGE_SERVICE + ".I" + serviceName + ";");
            bw.newLine();
            bw.newLine();

            // 构建控制层类注释
            BuildComment.createClassComment(bw, tableInfo.getComment() + "控制层");
            bw.newLine();

            bw.write("@RestController");
            bw.newLine();
            bw.write("@RequestMapping(\"/" + StringUtils.lowerCaseFirstLetter(beanName) + "\")");
            bw.newLine();
            bw.write("public class " + beanName + "Controller {");
            bw.newLine();
            bw.newLine();

            bw.write("\t@Autowired");
            bw.newLine();
            bw.write("\tprivate I" + serviceName + " " + lowServiceName + ";");
            bw.newLine();
            bw.newLine();

            // 新增业务
            bw.write("\t/**");  bw.newLine();
            bw.write("\t * 新增数据");  bw.newLine();
            bw.write("\t * @param " + lowBeanName);  bw.newLine();
            bw.write("\t * @return");  bw.newLine();
            bw.write("\t */");  bw.newLine();
            bw.write("\t@PostMapping(\"/save\")");  bw.newLine();
            bw.write("\tpublic R<Void> save(@RequestBody " + beanName + " " + lowBeanName + ") {");  bw.newLine();
            bw.write("\t\t" + lowServiceName + ".save(" + lowBeanName + ");");  bw.newLine();
            bw.write("\t\treturn R.ok();");  bw.newLine();
            bw.write("\t}");  bw.newLine();  bw.newLine();

            // 删除业务
            bw.write("\t/**");  bw.newLine();
            bw.write("\t * 根据id删除");  bw.newLine();
            bw.write("\t * @param " + id);  bw.newLine();
            bw.write("\t * @return");  bw.newLine();
            bw.write("\t */");  bw.newLine();
            bw.write("\t@DeleteMapping(\"/deleteById/{" + id + "}\")");  bw.newLine();
            bw.write("\tpublic R<Void> deleteById(@PathVariable(\"" + id + "\") Integer " + id + ") {");  bw.newLine();
            bw.write("\t\t" + lowServiceName + ".removeById(" + id + ");");  bw.newLine();
            bw.write("\t\treturn R.ok();");  bw.newLine();
            bw.write("\t}");  bw.newLine();  bw.newLine();

            // 修改业务
            bw.write("\t/**");  bw.newLine();
            bw.write("\t * 根据id修改数据");  bw.newLine();
            bw.write("\t * @param " + lowBeanName);  bw.newLine();
            bw.write("\t * @return");  bw.newLine();
            bw.write("\t */");  bw.newLine();
            bw.write("\t@PutMapping(\"/updateById\")");  bw.newLine();
            bw.write("\tpublic R<Void> updateById(@RequestBody " + beanName + " " + lowBeanName + ") {");  bw.newLine();
            bw.write("\t\t" + lowServiceName + ".updateById(" + lowBeanName + ");");  bw.newLine();
            bw.write("\t\treturn R.ok();");  bw.newLine();
            bw.write("\t}");  bw.newLine();  bw.newLine();

            // 查询业务
            bw.write("\t/**");  bw.newLine();
            bw.write("\t * 根据id查询数据");  bw.newLine();
            bw.write("\t * @param " + id);  bw.newLine();
            bw.write("\t * @return");  bw.newLine();
            bw.write("\t */");  bw.newLine();
            bw.write("\t@GetMapping(\"/getById/{" + id + "}\")");  bw.newLine();
            bw.write("\tpublic R<" + beanName + "> getById(@PathVariable(\"" + id + "\") Integer " + id + ") {");  bw.newLine();
            bw.write("\t\t" + beanName + " " + lowBeanName + " = " + lowServiceName + ".getById(" + id + ");");  bw.newLine();
            bw.write("\t\treturn R.ok(" + lowBeanName + ");");  bw.newLine();
            bw.write("\t}");  bw.newLine();

            bw.write("\t/**");  bw.newLine();
            bw.write("\t * 查询所有数据");  bw.newLine();
            bw.write("\t * @return");  bw.newLine();
            bw.write("\t */");  bw.newLine();
            bw.write("\t@GetMapping(\"/list\")");  bw.newLine();
            bw.write("\tpublic R<List<" + beanName + ">> list() {");  bw.newLine();
            bw.write("\t\tList<" + beanName +"> " + lowBeanName + "List = " + lowServiceName + ".list();");  bw.newLine();
            bw.write("\t\treturn R.ok(" + lowBeanName + "List" + ");");  bw.newLine();
            bw.write("\t}");  bw.newLine();  bw.newLine();



            bw.write("}");
            bw.flush();
        }   catch (Exception e) {
            logger.error("创建po失败", e);
        }  finally {
            if(outw !=null) {
                try {
                    outw.close();
                }   catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
